//
//  NewsMoreImageCell.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/9.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "NewsMoreImageCell.h"
#import "HomeSouceView.h"
#import "NewsDetailViewController.h"


@interface ImageItem : UICollectionViewCell
@property(nonatomic, strong)UIImageView *imageView;
@end

@interface MoreImageView : UIView
<
UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout
>

@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)UICollectionViewFlowLayout *flowLayout;

@property(nonatomic, strong)NSArray *imageArray;

@end

@interface NewsMoreImageCell ()
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)MoreImageView *moreView;
/// 时间 来源  标识
@property(nonatomic, strong)HomeSouceView *souceView;
///图片高度
@property(nonatomic, assign)CGFloat imageH;

@end

@implementation NewsMoreImageCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString * identifier = @"NewsMoreImageCell";
    NewsMoreImageCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[NewsMoreImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        
        self.imageH = (SCREEN_WIDTH - 32 - 10)/3 /1.5;

        self.titleLabel = [[UILabel alloc]init];
        self.titleLabel.font = [UIFont xm_medium:18];
        self.titleLabel.numberOfLines = 2;
        self.titleLabel.textColor = [UIColor xm_colorFromRGB:0x212832];
        [self addSubview:self.titleLabel];
        
        self.moreView = [[MoreImageView alloc]init];
        [self addSubview:self.moreView];
        

        self.souceView = [[HomeSouceView alloc]init];
        [self addSubview:self.souceView];
        
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer)];
        [self addGestureRecognizer:tapGesture];
        
    }
    return self;
}

-(void)tapGestureRecognizer{
    NewsDetailViewController * detailVC = [[NewsDetailViewController alloc]init];
    detailVC.model = self.model;
    [self.viewController.rt_navigationController pushViewController:detailVC animated:YES];
}


-(void)setModel:(AticleModel *)model{
    _model = model;
    self.titleLabel.text = _model.Title;
    self.souceView.model = _model;
    self.moreView.imageArray = [_model.ListImg componentsSeparatedByString:@","];
}

+ (CGFloat)heightWithModel:(AticleModel *)model{
    NewsMoreImageCell *cell = [[NewsMoreImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NewsMoreImageCell"];
    cell.model = model;
    [cell layoutIfNeeded];
    CGFloat imageH = (SCREEN_WIDTH - 32 - 10)/3 /1.5;
    return CGRectGetMaxY(cell.titleLabel.frame) + 20 + imageH + 60;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self).offset(18);
        make.right.equalTo(self).offset(-20);
    }];
    self.moreView.backgroundColor = XMRandomColor;
    [self.moreView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(16);
        make.right.mas_equalTo(self).offset(-16);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(20);
        make.height.mas_equalTo(self.imageH);
    }];
    
    [self.souceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.top.mas_equalTo(self.moreView.mas_bottom).offset(20);
        make.right.equalTo(self).offset(-20);
        make.height.mas_equalTo(@20);
    }];

}





@end












@implementation MoreImageView

-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.collectionView];
    }
    return self;
}
-(void)setImageArray:(NSArray *)imageArray{
    _imageArray = imageArray;
    [self.collectionView reloadData];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ImageItem * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageItem" forIndexPath:indexPath];
    [cell.imageView xm_setImageWithURL:self.imageArray[indexPath.row] placeholderImage:nil];
    return cell;
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((self.width - 10)/3, self.height);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}


-(void)layoutSubviews{
    [super layoutSubviews];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
}



-(UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        [_collectionView registerClass:[ImageItem class] forCellWithReuseIdentifier:@"ImageItem"];
    }
    return _collectionView;
}

-(UICollectionViewFlowLayout *)flowLayout{
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _flowLayout.minimumLineSpacing = 5;
        _flowLayout.minimumInteritemSpacing = 0;
    }
    return _flowLayout;
}


-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
}


@end





@implementation ImageItem

-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        self.imageView = [[UIImageView alloc] init];
        self.imageView.backgroundColor = XMRandomColor;
        [self addSubview:self.imageView];
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
}



@end


